/**
 *
 */
package com.comba.nutz.boot.demo.handler;

import com.comba.nutz.boot.demo.entity.ResultModel;
import org.nutz.json.Json;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.oauth2.provider.error.OAuth2AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 认证失败处理器
 *
 * @author zhailiang
 */
@Component("customAccessDeniedHandler")
public class CustomAccessDeniedHandler extends OAuth2AccessDeniedHandler {

    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response,
                       AccessDeniedException accessDeniedException) throws IOException, ServletException {
        response.setContentType("application/json;charset=utf-8");
        ResultModel resultModel = new ResultModel();

        resultModel.setMessage("access_token无效，请重新获取");
        resultModel.setData(accessDeniedException.getMessage());
        resultModel.setCode("-1");

        response.getWriter().write(Json.toJson(resultModel));
    }
}
